﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using BMG.BLL.Classes;

namespace BMG.BackOffice.UserControls
{
    /// <summary>
    /// Logique d'interaction pour HistoriqueManager.xaml
    /// </summary>
    public partial class HistoriqueManager : UserControl
    {
        public HistoriqueManager()
        {
            InitializeComponent();
            PopulateAndBind();
            
        }

        public void PopulateAndBind()
        {
            DataTable dtProduits = PRODUIT_MANAGER.GetHistorique(null, DateTime.MinValue, DateTime.Today.AddDays(1));
            listViewListeProduits.ItemsSource = dtProduits.DefaultView;

            DataTable dtCategories = CATEGORIE_MANAGER.GetHistorique(null, DateTime.MinValue, DateTime.Today.AddDays(1));
            listViewListeCat.ItemsSource = dtCategories.DefaultView;

            DataTable dtProduitsSelect = PRODUIT_MANAGER.GetList(false);
            cbbProduits.ItemsSource = dtProduitsSelect.DefaultView;

            DataTable dtCatSelect = CATEGORIE_MANAGER.GetList(false);
            cbbCategories.ItemsSource = dtCatSelect.DefaultView;

        }

        /// <summary>
        /// Evenement appelé par le clic du bouton de recherche
        /// </summary>
        /// <param name="sender">Objet déclencheur de l'événement</param>
        /// <param name="e">Paramètres de l'événement</param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            int? IDPro = (this.cbbProduits.SelectedValue == null) ? (int?) null : (int?)int.Parse(this.cbbProduits.SelectedValue.ToString());
            DateTime DateMin = this.cldDateMinProduit.SelectedDate ?? DateTime.MinValue;
            DateTime DateMax = this.cldDateMaxProduit.SelectedDate ?? DateTime.MaxValue;


           this.listViewListeProduits.ItemsSource = PRODUIT_MANAGER.GetHistorique(IDPro, DateMin, DateMax).DefaultView;

            
        }

        private void btnSearch_ClickCat(object sender, RoutedEventArgs e)
        {
            int? IDCat = this.cbbCategories.SelectedValue == null ? (int?)null : (int?)int.Parse(this.cbbCategories.SelectedValue.ToString());
            DateTime DateMin = this.cldDateMinCat.SelectedDate ?? DateTime.MinValue;
            DateTime DateMax = this.cldDateMaxCat.SelectedDate ?? DateTime.MaxValue;

            this.listViewListeCat.ItemsSource = CATEGORIE_MANAGER.GetHistorique(IDCat, DateMin, DateMax).DefaultView;

        }

        private void cbxAllModifsPro_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)cbxAllModifsPro.IsChecked)
            {
                cldDateMinProduit.IsEnabled = false;
                cldDateMaxProduit.IsEnabled = false;
                cldDateMinProduit.SelectedDate = DateTime.MinValue;
                cldDateMaxProduit.SelectedDate = DateTime.MaxValue;
            }
            else
            {
                cldDateMinProduit.IsEnabled = true;
                cldDateMaxProduit.IsEnabled = true;
                cldDateMinProduit.SelectedDate = DateTime.Today;
                cldDateMaxProduit.SelectedDate = DateTime.Today;
            }
        }

        private void cbxAllModifsCat_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)cbxAllModifsCat.IsChecked)
            {
                cldDateMinCat.IsEnabled = false;
                cldDateMaxCat.IsEnabled = false;
                cldDateMinCat.SelectedDate = DateTime.MinValue;
                cldDateMaxCat.SelectedDate = DateTime.MaxValue;
            }
            else
            {
                cldDateMinCat.IsEnabled = true;
                cldDateMaxCat.IsEnabled = true;
                cldDateMinCat.SelectedDate = DateTime.Today;
                cldDateMaxCat.SelectedDate = DateTime.Today;
            }
        }

    }
}
